package com.ui.mine.view;

import android.content.Intent;
import android.content.res.TypedArray;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Switch;

import com.base.BaseContract;
import com.base.BaseSlideOutActivity;
import com.constant.Constant;
import com.ddd.wandemo.R;
import com.event.ThemeEvent;
import com.util.SpUtil;
import com.widget.SelectThemeItem;

import org.greenrobot.eventbus.EventBus;

public class ThemeChangeActivity extends BaseSlideOutActivity {
    private Toolbar mToolbar;
    private Switch mSwiTheme;
    private boolean isNight;
    private LinearLayout mLlTheme;


    @Override
    protected int setLayoutId() {
        return R.layout.activity_theme_change;
    }

    @Override
    protected void initView() {
        isNight = SpUtil.getBoolean(Constant.IS_NIGHT);
        mToolbar = findViewById(R.id.toolbar);
        mSwiTheme = findViewById(R.id.swi_change);
        mSwiTheme.setChecked(isNight);
        mLlTheme = findViewById(R.id.ll_theme_list);
    }


    @Override
    protected void initEvent() {
        initToolBar();
        mSwiTheme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeTheme();
                startActivity(new Intent(ThemeChangeActivity.this,ThemeChangeActivity.class));
                overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
                EventBus.getDefault().post(new ThemeEvent(isNight));
                finish();
            }
        });

        if (!isNight) {
            //显示状态回显
            int themeIndex = SpUtil.getInt(Constant.THEME);
            ((SelectThemeItem)mLlTheme.getChildAt(themeIndex)).setThemeUse(mContext);
        }

        for (int i = 0;i<mLlTheme.getChildCount();i++){
            final SelectThemeItem childView = (SelectThemeItem)mLlTheme.getChildAt(i);
            final int finalI = i;
            childView.setOnClickThemeListener(new SelectThemeItem.OnClickListener() {
                @Override
                public void onThemeClick() {
                    //设置主题的index，重启activity，在baseactivity中获取主题index，动态设置主题
                    SpUtil.setInt(Constant.THEME, finalI);
                    startActivity(new Intent(ThemeChangeActivity.this,ThemeChangeActivity.class));
                    overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
                    EventBus.getDefault().post(new ThemeEvent(false));
                    finish();
                }
            });
        }
    }

    private void initToolBar() {
        mToolbar.setTitle(R.string.my_theme);
        //根据主题设置返回键
        TypedArray array = getTheme().obtainStyledAttributes(new int[] {
                R.attr.back_icon
        });
        int backIcon = array.getResourceId(0, 0);

        mToolbar.setNavigationIcon(backIcon);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }


    private void changeTheme() {
        isNight = !isNight;
        if (isNight) {
            AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_YES);
        }else {
            AppCompatDelegate.setDefaultNightMode(
                    AppCompatDelegate.MODE_NIGHT_NO);
        }

        int themeIndex = SpUtil.getInt(Constant.THEME);
        ((SelectThemeItem)mLlTheme.getChildAt(themeIndex)).setThemeUse(mContext);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected BaseContract.RootPresenter initPresenter() {
        return null;
    }
}
